﻿using CoreApi.Meta;
using MeasureDataHandler.Models;
using System;
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;

namespace MeasureDataHandler.Controllers
{
    public class DataHandleController : ApiController
    {
        private string _serviceAppKey = ConfigurationManager.AppSettings["service.appKey"] ?? "MEASUREDATAHANDLER";
        private string[] _clientAppKeys = (ConfigurationManager.AppSettings["client.appKey"] ?? string.Empty).Split(';');
        private string _uri = ConfigurationManager.AppSettings["service.uri"].ToString();
        private ReturnValue CheckAppKey(string key)
        {
            if (key == null || key.Equals(string.Empty)) return new ReturnValue(false,"AppKey.Missing");
            if (!_clientAppKeys.Contains<string>(key)) return new ReturnValue(false,"AppKey.Error");
            return new ReturnValue();
        }

        [HttpPost]
        public IHttpActionResult BloodPressureMeasureDataCollect([FromBody]BloodPressureMeasureDataRequest data)
        {
            try
            {
                ReturnValue rv=CheckAppKey(data.AppKey);
                if (!rv.Passed) return Ok<ReturnValue>(rv);
                data.AppKey = _serviceAppKey;
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(_uri);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var result= client.PostAsJsonAsync<BloodPressureMeasureDataRequest>("/api/DAQ/BloodPressureMeasureDataCollect", data).Result;
                    return ResponseMessage(result);
                }
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult GlycemicMeasureDataCollect([FromBody]GlycemicMeasureDataRequest data)
        {
            try
            {
                ReturnValue rv = CheckAppKey(data.AppKey);
                if (!rv.Passed) return Ok<ReturnValue>(rv);
                data.AppKey = _serviceAppKey;
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(_uri);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var result = client.PostAsJsonAsync<GlycemicMeasureDataRequest>("/api/DAQ/GlycemicMeasureDataCollect", data).Result;
                    return ResponseMessage(result);
                }
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }

        [HttpPost]
        public IHttpActionResult SmokeAlarmSignalCollect([FromBody]SmokeAlarmSignalRequest data)
        {
            try
            {
                ReturnValue rv = CheckAppKey(data.AppKey);
                if (!rv.Passed) return Ok<ReturnValue>(rv);
                data.AppKey = _serviceAppKey;
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(_uri);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var result = client.PostAsJsonAsync<SmokeAlarmSignalRequest>("/api/DAQ/SmokeAlarmSignalCollect", data).Result;
                    return ResponseMessage(result);
                }
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        

    }
}
